<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="/admin/include :: head">
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<section class="ftco-section">
    <div id="vueController" style="margin-left: 20px">
        <h2>方案管理</h2>
        <el-divider></el-divider>
        <el-button type="primary" @click="handleAddDialog()">新增</el-button>
        <el-table style="margin-top: 20px" :data.sync="dataList" border>
            <el-table-column prop="id" label="id" width="180">
            </el-table-column>
            <el-table-column prop="solutionType" label="方案类型" width="180" :formatter="getSolutionTypeNameById">
            </el-table-column>
            <el-table-column prop="title" label="方案标题" width="180">
            </el-table-column>
            <el-table-column prop="imgUrl" label="方案主图" width="180">
            </el-table-column>
            <el-table-column prop="order" label="顺序" width="180">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <a :href="'/solution/info/'+scope.row.id">
                        <el-button
                                size="mini"
                                type="danger">预览
                        </el-button>
                    </a>
                    <el-button
                            size="mini"
                            @click="handleEditDialog(scope.$index, scope.row)">编辑
                    </el-button>
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)">删除
                    </el-button>

                </template>
            </el-table-column>
        </el-table>
        <el-dialog :title="edit?'编辑方案':'新增方案'" :visible.sync="dialogFormVisible" fullscreen>
            <el-form :model="form">
                <el-form-item label="方案名称标题">
                    <el-input v-model="form.title" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="方案类型">
                    <el-select v-model="form.solutionType" placeholder="请选择方案类型">
                        <el-option
                                v-for="item in solutionTypeOptions"
                                :key="item.id"
                                :label="item.name"
                                :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="顺序">
                    <el-input v-model="form.order" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="方案主图">
                    <el-upload
                            action="/common/upload"
                            ref="upload"
                            :limit="1"
                            :file-list="fileList"
                            :on-success="handleUploadFinish"
                            list-type="picture">
                        <!--                    <i class="el-icon-upload"></i>-->
                        <el-button size="small" type="primary">点击上传</el-button>
                        <!--                    <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>-->
                    </el-upload>
                </el-form-item>
                <el-form-item label="方案详情文章">

                </el-form-item>
                <el-row :gutter="10">
                    <el-col :md="12" >
                        <div id="toolbar-container" class="toolbar sticky-top" style="border: #1E9FFF 1px solid"></div>
                        <div id="text-container" class="text border"></div>
                    </el-col>
                    <el-col :md="12" >
                        <div v-html="form.content" class="p-2"></div>
                    </el-col>
                </el-row>


            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleSubmit">提交</el-button>
            </div>
        </el-dialog>
    </div>
</section>

</body>
<script type="module">
    import {getSolutionInfoList, getSolutionTypeList, saveSolution, delSolution} from "/static/js/api/solutionApi.js";

    const E = window.wangEditor;
    var editor = undefined;
    new Vue({
        el: '#vueController',
        data() {
            return {
                dataList: [],
                fileList: [],
                dialogFormVisible: false,
                form: {},
                edit: false,
                solutionTypeOptions: [],
                formTemplate: {
                    id: "",
                    order: undefined,
                    imgUrl: "",
                    solutionType: undefined,
                    content: "",
                    title: "",
                },
            }
        },
        created() {
            this.getList();
            getSolutionTypeList().then(res => {
                this.solutionTypeOptions = res.data;
            })
        },
        methods: {
            getList() {
                getSolutionInfoList().then(res => {
                    console.log(res)
                    this.dataList = res.data;
                })
            },
            getSolutionTypeNameById(v){
                let name = "未知方案类型"
                this.solutionTypeOptions.forEach(item=>{
                    if (item.id === v.solutionType){
                        name = item.name
                    }
                })
                return name
            },
            handleUploadFinish(response, file, fileList) {
                console.log(response)
                this.form.imgUrl = response.data;
            },
            handleDelete(index, row) {
                delSolution(row.id).then(res => {
                    console.log("删除成功");
                    this.getList();
                })
            },
            handleSubmit() {
                this.form.contentIntroduction=editor.txt.text();
                saveSolution(this.form).then(res => {
                    console.log("添加成功");
                    this.getList();
                })
                this.fileList = [];
                this.dialogFormVisible = false;

            },
            handleAddDialog() {
                this.form = Object.assign({}, this.formTemplate);
                this.edit = false;
                this.dialogFormVisible = true;
                this.$nextTick(function() {
                    this.initEditor();
                })
                // $("#iframeMain").attr("src","/admin/product/productInfo");
            },
            handleEditDialog(index, row) {
                this.form = Object.assign({}, row);
                this.edit = true;
                this.dialogFormVisible = true;
                this.$nextTick(function() {
                    this.initEditor(this.form);
                })
            },
            initEditor(form){
                console.log(form)
                if (editor === undefined){
                    editor = new E('#toolbar-container', '#text-container') // 传入两个元素
                    // 自定义菜单配置
                    // 设置编辑区域高度为 500px
                    editor.config.height = 800
                    editor.config.zIndex = 10
                    editor.config.uploadImgShowBase64 = true
                    editor.config.onchange =  (html)=> {
                        // 第二步，监控变化，同步更新到 textarea
                        this.form.content = html
                    }
                    editor.create()
                }else {
                    editor.txt.clear()
                }
                // 初始化数据
                editor.txt.html(form.content?form.content:"")
            }
        },
        mounted() {

        }

    })
</script>
<style>
    /* table 样式 */
    table {
        border-top: 1px solid #ccc;
        border-left: 1px solid #ccc;
    }
    table td,
    table th {
        border-bottom: 1px solid #ccc;
        border-right: 1px solid #ccc;
        padding: 3px 5px;
    }
    table th {
        border-bottom: 2px solid #ccc;
        text-align: center;
    }

    /* blockquote 样式 */
    blockquote {
        display: block;
        border-left: 8px solid #d0e5f2;
        padding: 5px 10px;
        margin: 10px 0;
        line-height: 1.4;
        font-size: 100%;
        background-color: #f1f1f1;
    }

    /* code 样式 */
    code {
        display: inline-block;
        *display: inline;
        *zoom: 1;
        background-color: #f1f1f1;
        border-radius: 3px;
        padding: 3px 5px;
        margin: 0 3px;
    }
    pre code {
        display: block;
    }

    /* ul ol 样式 */
    ul, ol {
        margin: 10px 0 10px 20px;
    }
</style>
</html>